<?php
/**
 * 页面查看
 *
 */
// 加载公共文件
include dirname(__FILE__).'/global.php';
include_modules();

// 获取参数
$postid = isset($_GET['id']) ? intval(trim($_GET['id'])) : 0;
$page = isset($_GET['page']) ? intval(trim($_GET['page'])) : 1;

if (!$postid)
    die(__('Restricted access!'));
    

$post = post_get($postid);
if (!$post) {
    die(__('Restricted access!'));
}

$tpl  = tpl_init('post');
$b_guid = $inner = '';
comment_create($post['postid']); // 生成评论
// 处理文章
$post['list']     = taxonomy_get($post['listid']);
$post['cmt_path'] = post_get_path($post['listid'],$post['path'], C('CMT-Path'));
$post['path']     = post_get_path($post['listid'],$post['path']);
// 模版设置优先级：页面设置>分类设置>模型设置
if (empty($post['template'])) {
    if ($post['listid'] > 0) {
        $taxonomy = taxonomy_get($post['listid']);
        $post['template'] = $taxonomy['page'];
    }
    // 使用模型设置
    if (empty($post['template'])) {
        $model = model_get_bycode($post['model']);
        $post['template'] = $model['page'];
    }
}
// 加载模版
$html = tpl_loadfile(ABS_PATH.'/'.system_themes_path().'/'.esc_html($post['template']));

$vars = array(
    'postid'   => $post['postid'],
    'userid'   => $post['userid'],
    'author'   => $post['author'],
    'views'    => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_views&postid='.$post['postid'].'&updated=true"></script>',
    'comment'  => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_comment&postid='.$post['postid'].'"></script>',
    'people'   => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_comment_people&postid='.$post['postid'].'"></script>',
    'digg'     => $post['digg'],
    'date'     => $post['datetime'],
    'edittime' => $post['edittime'],
    'keywords' => post_get_taxonomy($post['keywords']),
    'prepage'  => post_prepage($post['listid'],$post['postid'],$preid),
    'nextpage' => post_nextpage($post['listid'],$post['postid'],$nextid),
    'cmt_state'    => $post['comments'],
    'cmt_ajaxinfo' => ROOT.'common/gateway.php?func=post_ajax_comment&postid='.$post['postid'],
    'cmt_replyurl' => ROOT.'common/gateway.php?func=post_send_comment&postid='.$post['postid'],
    'cmt_listsurl' => ROOT.$post['cmt_path'],
    'description'  => $post['description'],
);
// 设置分类变量
if (isset($post['list'])) {
    $vars['listid']     = $post['list']['taxonomyid'];
    $vars['listname']   = $post['list']['name'];
    $vars['listpath']   = ROOT.$post['list']['path'].'/';
    $vars['listcount']  = '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=taxonomy_count&listid='.$post['list']['taxonomyid'].'"></script>';
    if (isset($post['list']['meta'])) {
        foreach((array)$post['list']['meta'] as $k=>$v) {
            $vars['list.'.$k] = $v;
        }
    }
}
// 清理数据
tpl_clean($tpl);
tpl_set_var($b_guid, $inner, $tpl);
tpl_set_var($vars, $tpl);
// 设置自定义字段
if (isset($post['meta'])) {
    foreach((array)$post['meta'] as $k=>$v) {
        tpl_set_var('post.'.$k, $v, $tpl);
    }
}
// 文章导航
$guide = system_category_guide($post['listid']);

// 文章分页
if ($post['content'] && strpos($post['content'],'<!--pagebreak-->')!==false) {
    $contents = explode('<!--pagebreak-->',$post['content']);
    // 总页数
    $pages = count($contents);
    if (($pos=strrpos($post['path'],'.')) !== false) {
        $basename = substr($post['path'],0,$pos);
        $suffix   = substr($post['path'],$pos);
    } else {
        $basename = $post['path'];
        $suffix   = '';
    }
    if($page > $pages){
        $page = $pages;
    } elseif ($page <1) {
        $page = 1;
    }
    $content = $contents[$page-1];
    if ($page == 1) {
        $path  = $basename.$suffix;
        $title = $post['title'];
    } else {
        $path  = $basename.'_'.$page.$suffix;
        $title = $post['title'].' ('.$page.')';
        tpl_set_var('views', '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_views&postid='.$post['postid'].'"></script>', $tpl);
    }

    tpl_set_var(array(
        'guide'   => $guide ? $guide.' &gt;&gt; '.$title : $title,
        'title'   => $title,
        'content' => $content,
        'path'    => ROOT.$path,
    ), $tpl);
    $pagehtml = tpl_parse($html, $tpl);
    // 解析分页标签
    if (stripos($pagehtml,'{pagelist') !== false) {
        $pagehtml = preg_replace('/\{(pagelist)[^\}]*\/\}/isU',
            pages_list(ROOT.$basename.'_$'.$suffix, '!_$', $page, $pages, 1),
            $pagehtml
        );
    }
    echo $pagehtml;
}
// 没有分页
else {
    tpl_set_var(array(
        'guide'   => $guide ? $guide.' &gt;&gt; '.$post['title'] : $post['title'],
        'title'   => $post['title'],
        'content' => $post['content'],
        'path'    => ROOT.$post['path'],
    ), $tpl);
    // 解析分页标签
    if (stripos($html,'{pagelist') !== false) {
        $html = preg_replace('/\{(pagelist)[^\}]*\/\}/isU','',$html);
    }
    $html = tpl_parse($html, $tpl);
    echo $html;
}
